package com.example.teesystem.entityDto;

import com.example.teesystem.common.cache.UserCache;
import com.example.teesystem.common.utils.IssueUtil;
import com.example.teesystem.common.utils.TimeUtil;
import com.example.teesystem.entity.Class;
import com.example.teesystem.entity.ExamPaper;
import com.example.teesystem.entity.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : NieYaShi
 * @version : v1.0
 * @createTime : 2022/4/12 17:04
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExamPaperMessage {
    private Integer id;

    private String name;

    private Integer teacherId;

    private String teacherName;

    private String startTime = "";

    private String endTime = "";

    private Integer score;

    private String includeIssueType;

    private String includeIssueTypeName;

    private Integer isPublish;

    private String isPublishStr;

    private Integer isFinish;

    private String isFinishStr;

    private Integer isMark;

    private String isMarkStr;

    private Map<String, List<ExamIssueList>> listMap = new HashMap<>();

    private List<Class>classList = new ArrayList();

    public ExamPaperMessage(ExamPaper examPaper){
        BeanUtils.copyProperties(examPaper, this);
        if (examPaper.getStartTime() != null) {
            this.startTime = TimeUtil.dateTimeToString(examPaper.getStartTime());
        }
        if (examPaper.getEndTime() != null) {
            this.endTime = TimeUtil.dateTimeToString(examPaper.getEndTime());
        }
        User def = new User();
        def.setMyName("用户名未找到");
        this.teacherName = UserCache.userMap.getOrDefault(examPaper.getTeacherId(), def).getMyName();

        includeIssueType = includeIssueType.trim();
        if (!includeIssueType.equals("")) {
            String[] typeStrs = includeIssueType.split("\\$");
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < typeStrs.length; i++) {
                if (!typeStrs[i].equals(" ")) {
                    stringBuilder.append(IssueUtil.getIssueTypeName(Integer.parseInt(typeStrs[i])));
                    stringBuilder.append("、");
                }
            }
            stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
            this.includeIssueTypeName = stringBuilder.toString();
        }else {
            this.includeIssueTypeName = "";
        }

        if (isPublish == 0){
            this.isPublishStr = "未发布";
        }else {
            this.isPublishStr = "已发布";
        }

        if (isFinish == 0){
            this.isFinishStr = "未完成考试";
        }else {
            this.isFinishStr = "已考试结束";
        }

        if (isMark == 0){
            this.isMarkStr = "未完成阅卷";
        }else {
            this.isMarkStr = "已完成阅卷";
        }
    }
}
